/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmClienti.java
 *
 * Created on 28-lug-2011, 15.20.13
 */
package UI;

import UI.clienti.dialogs.dlgClientiSearch;
import UI.clienti.dialogs.dlgEditMails;
import UI.clienti.dialogs.dlgEditTelefoni;
import classes.Cliente;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author mauro
 */
public class frmClienti extends javax.swing.JFrame {

    /** Creates new form frmClienti */
    private Cliente Cli;
    public frmClienti() {
        initComponents();
        BufferedImage img = null;
        try{
            img = ImageIO.read(this.getClass().getResourceAsStream("/resources/clienti/identity.png"));
        }catch(Exception e){
            e.printStackTrace();
        }
        setIconImage(img);
        setLocation((Toolkit.getDefaultToolkit().getScreenSize().width - getSize().width)/2, (Toolkit.getDefaultToolkit().getScreenSize().height - getSize().height)/2);
        Cli = new Cliente();
        BoundCliente();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        P = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        txtNominativo = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        spIndirizzo = new javax.swing.JScrollPane();
        txtIndirizzo = new javax.swing.JTextArea();
        jLabel3 = new javax.swing.JLabel();
        spTelefoni = new javax.swing.JScrollPane();
        tabTelefoni = new javax.swing.JTable();
        jLabel4 = new javax.swing.JLabel();
        spMails = new javax.swing.JScrollPane();
        tabMails = new javax.swing.JTable();
        spNote = new javax.swing.JScrollPane();
        txtNote = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();
        cmEsci = new javax.swing.JButton();
        cmdNuovo = new javax.swing.JButton();
        cmdSalva = new javax.swing.JButton();
        cmdCancella = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();
        txtIDCliente = new javax.swing.JTextField();
        cmdAddTelefoni = new javax.swing.JButton();
        cmdAddMails = new javax.swing.JButton();
        cmdEditTelefoni = new javax.swing.JButton();
        cmdDeleteTelefoni = new javax.swing.JButton();
        cmdEditMails = new javax.swing.JButton();
        cmdDeleteMails = new javax.swing.JButton();
        cmdSerach = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Anagrafica Clienti");

        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel1.setText("Nominativo");

        jLabel2.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel2.setText("Indirizzo");

        txtIndirizzo.setColumns(20);
        txtIndirizzo.setRows(5);
        spIndirizzo.setViewportView(txtIndirizzo);

        jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel3.setText("Telefoni");

        tabTelefoni.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Tipo", "Numero", "Riferimento"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabTelefoni.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        tabTelefoni.getTableHeader().setReorderingAllowed(false);
        spTelefoni.setViewportView(tabTelefoni);
        tabTelefoni.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

        jLabel4.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel4.setText("E-mails");

        tabMails.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Indirizzo", "Riferimento"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabMails.setSelectionMode(javax.swing.ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        spMails.setViewportView(tabMails);

        txtNote.setColumns(20);
        txtNote.setRows(5);
        spNote.setViewportView(txtNote);

        jLabel5.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel5.setText("Note");

        cmEsci.setText("Esci");
        cmEsci.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmEsciActionPerformed(evt);
            }
        });

        cmdNuovo.setText("Nuovo");
        cmdNuovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdNuovoActionPerformed(evt);
            }
        });

        cmdSalva.setText("Salva");
        cmdSalva.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSalvaActionPerformed(evt);
            }
        });

        cmdCancella.setText("Cancella");
        cmdCancella.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancellaActionPerformed(evt);
            }
        });

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        jLabel6.setText("ID Cliente");

        txtIDCliente.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtIDClienteFocusLost(evt);
            }
        });

        cmdAddTelefoni.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/add.png"))); // NOI18N
        cmdAddTelefoni.setToolTipText("Aggiungi telefono");
        cmdAddTelefoni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAddTelefoniActionPerformed(evt);
            }
        });

        cmdAddMails.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/add.png"))); // NOI18N
        cmdAddMails.setToolTipText("Aggiungi indirizzo e-mail");
        cmdAddMails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAddMailsActionPerformed(evt);
            }
        });

        cmdEditTelefoni.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/edit.png"))); // NOI18N
        cmdEditTelefoni.setToolTipText("Modifica telefono");
        cmdEditTelefoni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEditTelefoniActionPerformed(evt);
            }
        });

        cmdDeleteTelefoni.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/remove.png"))); // NOI18N
        cmdDeleteTelefoni.setToolTipText("Cancella telefono");
        cmdDeleteTelefoni.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDeleteTelefoniActionPerformed(evt);
            }
        });

        cmdEditMails.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/edit.png"))); // NOI18N
        cmdEditMails.setToolTipText("Modifica indirizzo e-mail");
        cmdEditMails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdEditMailsActionPerformed(evt);
            }
        });

        cmdDeleteMails.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/remove.png"))); // NOI18N
        cmdDeleteMails.setToolTipText("Cancella indirizzo e-mail");
        cmdDeleteMails.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdDeleteMailsActionPerformed(evt);
            }
        });

        cmdSerach.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/common/search.png"))); // NOI18N
        cmdSerach.setToolTipText("Ricerca utente");
        cmdSerach.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdSerachActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout PLayout = new javax.swing.GroupLayout(P);
        P.setLayout(PLayout);
        PLayout.setHorizontalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, PLayout.createSequentialGroup()
                        .addComponent(cmdNuovo)
                        .addGap(18, 18, 18)
                        .addComponent(cmdSalva)
                        .addGap(18, 18, 18)
                        .addComponent(cmdCancella)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 248, Short.MAX_VALUE)
                        .addComponent(cmEsci))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, PLayout.createSequentialGroup()
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jLabel6, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel5, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel4, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(PLayout.createSequentialGroup()
                                .addComponent(txtIDCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(cmdSerach, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(spIndirizzo, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                            .addComponent(txtNominativo, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                            .addComponent(spNote, javax.swing.GroupLayout.DEFAULT_SIZE, 481, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PLayout.createSequentialGroup()
                                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addComponent(spTelefoni, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE)
                                    .addComponent(spMails, javax.swing.GroupLayout.DEFAULT_SIZE, 457, Short.MAX_VALUE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(cmdEditMails, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(cmdAddTelefoni, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(cmdAddMails, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(cmdEditTelefoni, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(cmdDeleteTelefoni, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                    .addComponent(cmdDeleteMails, javax.swing.GroupLayout.PREFERRED_SIZE, 18, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addContainerGap())
        );
        PLayout.setVerticalGroup(
            PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, PLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(txtIDCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cmdSerach, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtNominativo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(spIndirizzo, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE))
                .addGap(32, 32, 32)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3)
                    .addComponent(spTelefoni, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdAddTelefoni, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdEditTelefoni, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdDeleteTelefoni, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(20, 20, 20)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(PLayout.createSequentialGroup()
                        .addComponent(cmdAddMails, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdEditMails, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cmdDeleteMails, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(spMails, javax.swing.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                    .addComponent(jLabel4))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(spNote, javax.swing.GroupLayout.DEFAULT_SIZE, 98, Short.MAX_VALUE)
                    .addComponent(jLabel5))
                .addGap(18, 18, 18)
                .addGroup(PLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmEsci)
                    .addComponent(cmdNuovo)
                    .addComponent(cmdSalva)
                    .addComponent(cmdCancella))
                .addContainerGap())
        );

        getContentPane().add(P, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void cmEsciActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmEsciActionPerformed
        setVisible(false);
        dispose();
    }//GEN-LAST:event_cmEsciActionPerformed

    private void cmdNuovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdNuovoActionPerformed
        Cli = new Cliente();
        BoundCliente();
    }//GEN-LAST:event_cmdNuovoActionPerformed

    private void cmdSalvaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSalvaActionPerformed
        if(ControllaForm()){
            if(Cli.save()){
                JOptionPane.showMessageDialog(this, "Salvataggio effettuato", "TheWareHouser", JOptionPane.INFORMATION_MESSAGE);
            }else{
                JOptionPane.showMessageDialog(this, "Salvataggio non effettuato,errore sul database", "TheWareHouser", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_cmdSalvaActionPerformed

    private void cmdCancellaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancellaActionPerformed
        if(Cli.getIDCliente() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Cancellare il cliente selezionato e tutti i relativi dati?", "The WareHouse", JOptionPane.QUESTION_MESSAGE) == JOptionPane.YES_OPTION){
                Cli.delete();
            }
        }else{
            Cli = new Cliente();
            BoundCliente();
        }
    }//GEN-LAST:event_cmdCancellaActionPerformed

    private void txtIDClienteFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtIDClienteFocusLost
        if(!txtIDCliente.getText().equals("")){
            try{    
                Cli = new Cliente();
                int numID = Integer.parseInt(txtIDCliente.getText().trim());
                Cli.setIDCLiente(numID);
                Cli.load();
                BoundCliente();
            }catch(Exception e){
                JOptionPane.showMessageDialog(this, "Attenzione, l'ID deve essere numerico", "TheWareHouser", JOptionPane.ERROR_MESSAGE);
            }
        }
    }//GEN-LAST:event_txtIDClienteFocusLost

    private void cmdAddTelefoniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddTelefoniActionPerformed
        ((DefaultTableModel)tabTelefoni.getModel()).addRow(new Object[tabTelefoni.getColumnCount()]);
        tabTelefoni.getSelectionModel().setSelectionInterval(tabTelefoni.getRowCount()-1, tabTelefoni.getRowCount()-1);
        tabTelefoni.setValueAt("", tabTelefoni.getSelectedRow(), 0);
        tabTelefoni.setValueAt("", tabTelefoni.getSelectedRow(), 1);
        tabTelefoni.setValueAt("", tabTelefoni.getSelectedRow(), 2);
        dlgEditTelefoni Dlg = new dlgEditTelefoni(this, tabTelefoni, true);
        Dlg.setVisible(true);
    }//GEN-LAST:event_cmdAddTelefoniActionPerformed

    private void cmdEditTelefoniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEditTelefoniActionPerformed
        if(tabTelefoni.getSelectedRow() >= 0){
            dlgEditTelefoni Dlg = new dlgEditTelefoni(this, tabTelefoni, true);
            Dlg.setVisible(true);
        }
    }//GEN-LAST:event_cmdEditTelefoniActionPerformed

    private void cmdDeleteTelefoniActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteTelefoniActionPerformed
        if(tabTelefoni.getSelectedRow() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Cancellare il numero selezionato?", "The WareHouse", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                ((DefaultTableModel)tabTelefoni.getModel()).removeRow(tabTelefoni.getSelectedRow());
            }
        }
    }//GEN-LAST:event_cmdDeleteTelefoniActionPerformed

    private void cmdAddMailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAddMailsActionPerformed
        ((DefaultTableModel)tabMails.getModel()).addRow(new Object[tabMails.getColumnCount()]);
        tabMails.getSelectionModel().setSelectionInterval(tabMails.getRowCount()-1, tabMails.getRowCount()-1);
        tabMails.setValueAt("", tabMails.getSelectedRow(), 0);
        tabMails.setValueAt("", tabMails.getSelectedRow(), 1);
        dlgEditMails Dlg = new dlgEditMails(this, tabMails, true);
        Dlg.setVisible(true);
    }//GEN-LAST:event_cmdAddMailsActionPerformed

    private void cmdEditMailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdEditMailsActionPerformed
        if(tabMails.getSelectedRow() >= 0){
            dlgEditMails Dlg = new dlgEditMails(this, tabMails, true);
            Dlg.setVisible(true);
        }
    }//GEN-LAST:event_cmdEditMailsActionPerformed

    private void cmdDeleteMailsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdDeleteMailsActionPerformed
        if(tabMails.getSelectedRow() >= 0){
            if(JOptionPane.showConfirmDialog(this, "Cancellare l'indirizzo selezionato?", "The WareHouse", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
                ((DefaultTableModel)tabMails.getModel()).removeRow(tabMails.getSelectedRow());
            }
        }
    }//GEN-LAST:event_cmdDeleteMailsActionPerformed

    private void cmdSerachActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdSerachActionPerformed
        dlgClientiSearch Search = new dlgClientiSearch(this, true);
        Search.setVisible(true);
        Cli.load();
        BoundCliente();
    }//GEN-LAST:event_cmdSerachActionPerformed

    
    private void BoundCliente(){
        txtIDCliente.setText("");
        if(Cli.getIDCliente() >= 0)txtIDCliente.setText(""+Cli.getIDCliente());
        txtNominativo.setText(Cli.getRagioneSociale());
        txtIndirizzo.setText(Cli.getIndirizzo());
        txtNote.setText(Cli.getNote());
        String Str_telefoni = Cli.getTelefoni();
        ((DefaultTableModel)tabTelefoni.getModel()).setRowCount(0);
        while(Str_telefoni.contains(";")){
            String temp = Str_telefoni.substring(0,Str_telefoni.indexOf(";"));
            Str_telefoni = Str_telefoni.substring(Str_telefoni.indexOf(";")+1);
            ((DefaultTableModel)tabTelefoni.getModel()).addRow(new Object[tabTelefoni.getColumnCount()]);
            tabTelefoni.setValueAt(temp.substring(0,temp.indexOf("\t")), tabTelefoni.getRowCount()-1, 0);
            temp = temp.substring(temp.indexOf("\t")+1);
            tabTelefoni.setValueAt(temp.substring(0,temp.indexOf("\t")), tabTelefoni.getRowCount()-1, 1);
            tabTelefoni.setValueAt(temp.substring(temp.indexOf("\t")+1), tabTelefoni.getRowCount()-1, 2);
        }
        if(Str_telefoni.length() > 0){
            ((DefaultTableModel)tabTelefoni.getModel()).addRow(new Object[tabTelefoni.getColumnCount()]);
            tabTelefoni.setValueAt(Str_telefoni.substring(0,Str_telefoni.indexOf("\t")), tabTelefoni.getRowCount()-1, 0);
            Str_telefoni = Str_telefoni.substring(Str_telefoni.indexOf("\t")+1);
            tabTelefoni.setValueAt(Str_telefoni.substring(0,Str_telefoni.indexOf("\t")), tabTelefoni.getRowCount()-1, 1);
            tabTelefoni.setValueAt(Str_telefoni.substring(Str_telefoni.indexOf("\t")+1), tabTelefoni.getRowCount()-1, 2);
        }
        Str_telefoni = "";
        String Str_mails = Cli.getMail();
        ((DefaultTableModel)tabMails.getModel()).setRowCount(0);
        while(Str_mails.contains(";")){
            String temp = Str_mails.substring(0,Str_mails.indexOf(";"));
            Str_mails = Str_mails.substring(Str_mails.indexOf(";")+1);
            ((DefaultTableModel)tabMails.getModel()).addRow(new Object[tabMails.getColumnCount()]);
            tabMails.setValueAt(temp.substring(0,temp.indexOf("\t")), tabMails.getRowCount()-1, 0);
            tabMails.setValueAt(temp.substring(temp.indexOf("\t")+1), tabMails.getRowCount()-1, 1);
        }
        if(Str_mails.length() > 0){
            ((DefaultTableModel)tabMails.getModel()).addRow(new Object[tabMails.getColumnCount()]);
            tabMails.setValueAt(Str_mails.substring(0,Str_mails.indexOf("\t")), tabMails.getRowCount()-1, 0);
            tabMails.setValueAt(Str_mails.substring(Str_mails.indexOf("\t")+1), tabMails.getRowCount()-1, 1);            
        }
        Str_mails = "";
    }
    
    public boolean ControllaForm(){
        if(txtNominativo.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Inserire un nominativo valido", "The WareHouse", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        if(txtIndirizzo.getText().trim().equals("")){
            JOptionPane.showMessageDialog(this, "Inserire un indirizzo valido", "The WareHouse", JOptionPane.WARNING_MESSAGE);
            return false;
        }
        
        Cli.setRagioneSociale(txtNominativo.getText());
        Cli.setIndirizzo(txtIndirizzo.getText());
        Cli.setNote(txtNote.getText());
        
        String Telefoni = "";
        for(int i = 0;i<tabTelefoni.getRowCount();i++){
            Telefoni = Telefoni + tabTelefoni.getValueAt(i, 0).toString() + "\t";
            Telefoni = Telefoni + tabTelefoni.getValueAt(i, 1).toString() + "\t";
            Telefoni = Telefoni + tabTelefoni.getValueAt(i, 2).toString() + ";";
        }
        Cli.setTelefoni(Telefoni);
        
        String Mails = "";
        for(int i=0;i<tabMails.getRowCount();i++){
            Mails = Mails + tabMails.getValueAt(i, 0).toString() + "\t";
            Mails = Mails + tabMails.getValueAt(i, 1).toString() + ";";
        }
        Cli.setMail(Mails);
        
        return true;
    }
    
    public Cliente getClienteClass(){
        return Cli;
    }
    
    public void setClienteClass(Cliente C){
        Cli = C;
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel P;
    private javax.swing.JButton cmEsci;
    private javax.swing.JButton cmdAddMails;
    private javax.swing.JButton cmdAddTelefoni;
    private javax.swing.JButton cmdCancella;
    private javax.swing.JButton cmdDeleteMails;
    private javax.swing.JButton cmdDeleteTelefoni;
    private javax.swing.JButton cmdEditMails;
    private javax.swing.JButton cmdEditTelefoni;
    private javax.swing.JButton cmdNuovo;
    private javax.swing.JButton cmdSalva;
    private javax.swing.JButton cmdSerach;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane spIndirizzo;
    private javax.swing.JScrollPane spMails;
    private javax.swing.JScrollPane spNote;
    private javax.swing.JScrollPane spTelefoni;
    private javax.swing.JTable tabMails;
    private javax.swing.JTable tabTelefoni;
    private javax.swing.JTextField txtIDCliente;
    private javax.swing.JTextArea txtIndirizzo;
    private javax.swing.JTextField txtNominativo;
    private javax.swing.JTextArea txtNote;
    // End of variables declaration//GEN-END:variables
}
